/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cas.struts.action;

import com.cas.dbaccess.CASDBAccessfactory;
import com.cas.hibernate.entity.Appointment;
import com.cas.hibernate.entity.Doctor;
import com.cas.hibernate.entity.Patient;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

/**
 *
 * @author suraj
 */
public class MenuAction extends org.apache.struts.action.Action {

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String menuOption = request.getParameter("menuOption");
        System.out.println("Selected option is : " + menuOption);
        
        if("viewDoctors".equals(menuOption))
        {
            getDoctors(request);
        }
        else if ("viewDoctorsSortID".equals(menuOption)) {
            getDoctorsSort(request, "id");
        } 
        else if ("viewDoctorsSortName".equals(menuOption)) {
            getDoctorsSort(request, "firstName");
        }
        else if("viewPatients".equals(menuOption))
        {
            getPatients(request);
        }
        else if("viewAppointments".equals(menuOption))
        {
            getAppointments(request);
        }
        else if("viewMessages".equals(menuOption))
        {
            getUserMessages(request);
        }

        return mapping.findForward(menuOption);
    }
    
    public void getDoctorsSort(HttpServletRequest request, String key)
    {
        List<Doctor> doctors = CASDBAccessfactory.getCASDbAccess().getAllDoctors();
        BeanComparator comparator = new BeanComparator(key);
            Collections.sort( doctors, comparator );
        request.setAttribute("listDoctors", doctors);
    }
     public void getDoctors(HttpServletRequest request)
    {
        List<Doctor> doctors = CASDBAccessfactory.getCASDbAccess().getAllDoctors();
        request.setAttribute("listDoctors", doctors);
    }
    
    public void getPatients(HttpServletRequest request)
    {
        List<Patient> patients = CASDBAccessfactory.getCASDbAccess().getAllPatients();
        request.setAttribute("listPatients", patients);
    }

    public void getAppointments(HttpServletRequest request)
    {
        List<Appointment> appointments = CASDBAccessfactory.getCASDbAccess().getAllAppointments();
        request.setAttribute("listAppointments", appointments);
    }

    public void getUserMessages(HttpServletRequest request)
    {
        List<Appointment> appointments = CASDBAccessfactory.getCASDbAccess().getAllAppointments();
        request.setAttribute("listMessages", appointments);
    }
    
}
